<?php

	////////////////////////////////////////////////////////////////////////////////
	//
	// swxformat.org Flickr Upload script by Aral Balkan.
	// Copyright (c) 2007 Aral Balkan. Released under the MIT license. 
	//
	// Used with the SWX Flickr API for initiating photo uploads to Flickr. 
	// This is outside the SWX Flickr API as it requires binary uploads to Flickr.
	// 
	// You cannot upload photos to Flickr directly from Flash as the FileReference 
	// call is not compatible with Flickr's API signing. (You will get an 
	// "Invalid signature" error.) Use this script instead to upload your photos.
	// 
	// Hit this script with a POST request from FileReference in Flash. 
	// 
	////////////////////////////////////////////////////////////////////////////////

	// Define E_STRICT if not defined so we don't get errors on PHP 4
	if (!defined('E_STRICT')) define('E_STRICT', 2048);
	
	// Error handling
	function errorHandler($errorNum, $errorStr, $errorFile, $errorLine)
	{
		$errorMsg = "Error $errorNum: $errorStr in $errorFile, line $errorLine.";
		$GLOBALS['swxLastErrorMessage'] = $errorMsg;
		
		// Display the error message in the PHP error log
		error_log($errorMsg);
		
		$errorObj = array('error' => TRUE, 'code' => $errorNum, 'message' => $errorMsg);
		
		//if ($errorNum == E_ERROR || $errorNum == E_WARNING || $errorNum = E_USER_ERROR)
		// Error num check replaced by code from http://drupal.org/node/11772#comment-18383.
		// This stops PHP5 strict errors from failing a call (e.g., deprecated calls, etc.)
		//if (($errorNum & (E_ALL & E_STRICT) ^ (E_NOTICE & E_STRICT)) || $errorNum = E_USER_ERROR)
		if ($errorNum != E_STRICT)
		{			
			// On errors and warnings, stop execution and return the
			// error message to the client. This is a far better
			// alternative to failing silently.
			returnError($errorObj);
		}
	}
	
	/**
	 * Returns an error message.
	 *
	 * @param	(str) The error message to return to the client.
	 * 
	 * @return void (exits)
	 * @author Aral Balkan
	 **/
	function returnError($errorObj)
	{
		global $swfCompiler, $debug;
		
		if (strpos($errorObj['message'], 'File to upload is missing.') != false)
		{
			echo ("Please hit this URL with a FileReference upload() request from Flash.");
		}
		else
		{
			echo ($errorObj['message']);
		}
		
		error_log($errorObj['message']);
		
		exit();		
	}
	

	/**
	 * Returns a result message
	 *
	 * @param string $result 
	 * @author Aral Balkan
	 */
	function returnResult($result)
	{
		global $api;
		
		if ($result === false)
		{
			// Send the meaningful error message returned by Flickr, instead of false.
			echo ('Flickr error '.$api->error_code.': '.$api->error_msg);
		}
		else
		{
			echo $result;
		}
		
		exit();
	}

	// Error handling (unfortunately has to be global to support PHP 4)
	set_error_handler('errorHandler');

	// Turn on error reporting
	error_reporting(E_ALL);

	// Use the phpFlickr library. 
	include('../../php/lib/phpFlickr/phpFlickr.php');

	$API_KEY = "e7efb59164979981686e62d8bcc473be";
	$SHARED_SECRET = "2be064bed40b0b78";
			
	$api = new phpFlickr($API_KEY, $SHARED_SECRET);
	
	if (isset($_FILES['photo']))
	{
		$photo = $_FILES['photo']['tmp_name'];
	}
	else
	{
		trigger_error('File to upload is missing.', E_USER_ERROR);
	}

	if (isset($_POST['token']))
	{
		$api->setToken($_POST['token']);
	}
	else
	{
		trigger_error('No auth token passed. Uploads require a valid token with write authorization.');
	}

	$title = isset($_POST['title']) ? $_POST['title'] : NULL;
	$description = isset($_POST['description']) ? $_POST['description'] : NULL;
	$tags = isset($_POST['tags']) ? $_POST['tags'] : NULL;
	$isPublic = isset($_POST['isPublic']) ? $_POST['isPublic'] : NULL;
	$isFriend = isset($_POST['isFriend']) ? $_POST['isFriend'] : NULL;
	$isFamily = isset($_POST['isFamily']) ? $_POST['isFamily'] : NULL;
	
	$async = isset($_POST['async']);
	
	$result = '';
	
	if ($async)
	{
		// Do an asynchronous upload.
		$result = $api->async_upload ($photo, $title, $description, $tags, $isPublic, $isFriend, $isFamily);  
	}
	else
	{
		// Do a synchronous upload.
		$result = $api->sync_upload ($photo, $title, $description, $tags, $isPublic, $isFriend, $isFamily); 
	}
	
	returnResult($result);
?>